﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace NDK.Motion.MachineResources
{
    [Serializable]
    /// <summary>
    /// 运动点位表
    /// </summary>
    public class Position
    {
        private string _guid;
        private string _positionName;
        private int _positionOrder;
        private string _axisPriority;
        private bool _isRun;
        private string _function;
        private string _description;
        private string _station;
        private bool _isAllowUniform;
        public List<Axis> AxisList = new List<Axis>();
        public List<VelocityProfile> VelocityProfiles = new List<VelocityProfile>();

        /// <summary>
        /// 点位运行顺序
        /// </summary>
        public int PositionOrder
        {
            get
            {
                return _positionOrder;
            }

            set
            {
                _positionOrder = value;
            }
        }
        /// <summary>
        /// 轴优先级
        /// </summary>
        public string AxisPriority
        {
            get
            {
                return _axisPriority;
            }

            set
            {
                _axisPriority = value;
            }
        }
        /// <summary>
        /// 点位是否运行
        /// </summary>
        public bool IsRun
        {
            get
            {
                return _isRun;
            }

            set
            {
                _isRun = value;
            }
        }

        /// <summary>
        /// 点位功能
        /// </summary>
        public string Function
        {
            get
            {
                return _function;
            }

            set
            {
                _function = value;
            }
        }
        /// <summary>
        /// 点位备注描述
        /// </summary>
        public string Description
        {
            get
            {
                return _description;
            }

            set
            {
                _description = value;
            }
        }
        /// <summary>
        /// 点位所在工位
        /// </summary>
        public string Station
        {
            get
            {
                return _station;
            }

            set
            {
                _station = value;
            }
        }
        /// <summary>
        /// 点位的唯一标识，
        /// </summary>
        public string Guid
        {
            get
            {
                return _guid;
            }

            set
            {
                _guid = value;
            }
        }
        /// <summary>
        /// 点位名称
        /// </summary>
        public string PositionName
        {
            get
            {
                return _positionName;
            }

            set
            {
                _positionName = value;
            }
        }

        public bool IsAllowUniform
        {
            get
            {
                return _isAllowUniform;
            }

            set
            {
                _isAllowUniform = value;
            }
        }

        public override string ToString()
        {
            return PositionName + "," + PositionOrder + "," + AxisPriority + "," + Function + ","
                + Description + "," + IsRun + "," + Station;
        }

        /// <summary>
        /// 深度复制当前对象
        /// </summary>
        /// <returns></returns>
        public Position DeepCopy()
        {
            object retval;
            using (MemoryStream ms = new MemoryStream())
            {
                BinaryFormatter bf = new BinaryFormatter();
                //将当前对象序列化
                bf.Serialize(ms, this);
                ms.Seek(0, SeekOrigin.Begin);
                //反序列化成对象
                retval = bf.Deserialize(ms);
                ms.Close();
            }
            Position pos = (Position)retval;
            pos.Guid = System.Guid.NewGuid().ToString();
            return pos;
        }
        public Position Clone()
        {
            Position position = new Position();
            position.Guid = _guid;
            position.PositionName = _positionName;
            position.PositionOrder = _positionOrder;
            position.AxisPriority = _axisPriority;
            position.IsRun = _isRun;
            position.IsAllowUniform = _isAllowUniform;
            position.Function = _function;
            position.Description = _description;
            position.Station = _station;

            position.AxisList = AxisList;

            foreach (var velocityProfiles in VelocityProfiles)
            {
                VelocityProfile vp = velocityProfiles.Clone();
                position.VelocityProfiles.Add(vp);
            }

            return position;
        }

        public void SetPos(string AxisName, double offset)
        {
            try
            {
                var vpList = VelocityProfiles.FindAll(x => x.AxisName == AxisName);
                if (vpList == null)
                {
                    throw new Exception($"设置点位失败,未找到轴{AxisName}");
                }
                if (vpList.Count > 1)
                {
                    throw new Exception($"设置点位失败,轴{AxisName}名称重复多个");
                }
                VelocityProfile vp = vpList[0];

                vp.Pos += offset;
            }
            catch (Exception ex)
            {
                throw new Exception($"设置点位异常" + ex.Message + ex.StackTrace);
            }
           

        }

        public double GetPos(string AxisName)
        {
            try
            {
                var vpList = VelocityProfiles.FindAll(x => x.AxisName == AxisName);
                if (vpList == null)
                {
                    throw new Exception($"获取点位失败,未找到轴{AxisName}");
                }
                if (vpList.Count > 1)
                {
                    throw new Exception($"获取点位失败,轴{AxisName}名称重复多个");
                }
                VelocityProfile vp = vpList[0];

                return vp.Pos;
            }
            catch (Exception ex)
            {
                throw new Exception($"设置点位异常" + ex.Message + ex.StackTrace);
            }
        }
    }
}
